package main

import (
	"fmt"
	"github.com/tebeka/selenium"
	"github.com/tebeka/selenium/chrome"
	"log"
	"time"
)

func chromeDriver() {
	// 启动 Chrome 浏览器
	opts := []selenium.ServiceOption{
		selenium.ChromeDriver("/usr/local/bin/chromedriver"), // 指定 chromedriver 所在路径
	}
	service, err := selenium.NewChromeDriverService("/usr/local/bin/chromedriver", 9515, opts...)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer service.Stop()

	caps := selenium.Capabilities{
		"browserName": "chrome",
	}
	chromeOpts := chrome.Capabilities{
		Args: []string{
			//	"--headless", // 无头模式
		},
	}
	caps.AddChrome(chromeOpts)

	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", 9515))
	if err != nil {
		log.Fatal(err)
		return
	}

	cookie := selenium.Cookie{
		Name:   "JSESSIONID",
		Value:  "0A8D532A4B4CECC4F701E000035056C8",
		Path:   "/",
		Domain: "www.allcpp.cn",
		Expiry: 10000000000,
	}
	wd.AddCookie(&cookie)

	cookie1 := selenium.Cookie{
		Name:   "JALKSJFJASKDFJKALSJDFLJSF",
		Value:  "2392071151b782ecc8ab784857bd845d3915e9a36f10.92.108.68_10522398",
		Path:   "/",
		Domain: ".allcpp.cn",
		Expiry: 10000000000,
	}
	wd.AddCookie(&cookie1)

	cookie2 := selenium.Cookie{
		Name:   "Hm_lpvt_75e110b2a3c6890a57de45bd2882ec7c",
		Value:  "1682126895.92.108.68_10522398",
		Path:   "/",
		Domain: ".allcpp.cn",
		Expiry: 10000000000,
	}
	wd.AddCookie(&cookie2)

	cookie3 := selenium.Cookie{
		Name:   "token",
		Value:  "REvR5a13NVjGsMQ5vWEMLTtVTjNeF5gLIyUqI2a/M3c9M0rCE6wJ0Q0NhO2DJuXSzLur41+gYvQg1xi9D2TrhX+bgwef35sswtufzm8hA12Aywv84wBmTWcigueSaFahrptFE0s7ZFCk+fNX0m6uVUXBQV0Q1kc39259SQvKONg=",
		Path:   "/",
		Domain: ".allcpp.cn",
		Expiry: 10000000000,
	}
	wd.AddCookie(&cookie3)

	cookie4 := selenium.Cookie{
		Name:   "Hm_lvt_75e110b2a3c6890a57de45bd2882ec7c",
		Value:  "1682046786",
		Path:   "/",
		Domain: ".allcpp.cn",
		Expiry: 10000000000,
	}
	wd.AddCookie(&cookie4)

	cookie5 := selenium.Cookie{
		Name:   "Hm_lpvt_9844bb5d031f8444d57d8727d532b340",
		Value:  "1682242169",
		Path:   "/",
		Domain: ".cp.allcpp.cn",
		Expiry: 10000000000,
	}
	wd.AddCookie(&cookie5)

	cookie6 := selenium.Cookie{
		Name:   "Hm_lvt_9844bb5d031f8444d57d8727d532b340",
		Value:  "1682046796",
		Path:   "/",
		Domain: ".cp.allcpp.cn",
		Expiry: 10000000000,
	}
	wd.AddCookie(&cookie6)
	wd.NewSession()
	defer wd.Quit()

	// 打开指定网页并获取网页内容
	if err := wd.Get("https://cp.allcpp.cn/#/ticket/detail?event=1074"); err != nil {
		log.Fatal(err)
		return
	}
	html, err := wd.PageSource()
	if err != nil {
		log.Fatal(err)
		return
	}

	// 模拟用户操作
	buttonTickets, err := wd.FindElements(selenium.ByClassName, "ticket-box")
	for {
		fmt.Println("FindElements err=%v", err)
		if err != nil || len(buttonTickets) == 0 {
			fmt.Println("FindElements err=%v", err)
			time.Sleep(1 * time.Minute)
			wd.Get("https://cp.allcpp.cn/#/ticket/detail?event=1074")
			buttonTickets, err = wd.FindElements(selenium.ByClassName, "ticket-box")
		} else {
			break
		}
	}

	//
	for _, buttonElement := range buttonTickets {

		buttonElement.Click()
		time.Sleep(5 * time.Second)
	}

	/*// 模拟用户操作
	searchBox, err := wd.FindElement(selenium.ByCSSSelector, "#kw")
	if err != nil {
		time.Sleep(1 * time.Hour)
		fmt.Println(err)
		return
	}
	searchBox.SendKeys("golang")
	submitBtn, err := wd.FindElement(selenium.ByCSSSelector, "#su")
	if err != nil {
		fmt.Println(err)
		return
	}
	submitBtn.Click()
	time.Sleep(5 * time.Second)
	html, err = wd.PageSource()
	if err != nil {
		fmt.Println(err)
		return
	}
	*/
	fmt.Println(html)

}
